package com.mikasa.springboot.example.redis;

import com.alibaba.fastjson.JSON;
import com.mikasa.springboot.example.domain.RedisMessage;
import com.mikasa.springboot.example.redismq.UserMessageCustomer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by sherlock on 2016/10/25.
 */
public class RedisListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(RedisListener.class);

    private static final ExecutorService threadPool = Executors.newFixedThreadPool(10);

    @Autowired
    private UserMessageCustomer userMessageCustomer;

    /*private CountDownLatch latch;

    @Autowired
    public RedisListener(CountDownLatch latch) {
        this.latch = latch;
    }*/

    @Autowired
    public RedisListener() {
    }

    public void onMessage(String message,String channel) {
        LOGGER.info("message <" + message + ">");
        LOGGER.info("channel <" + channel + ">");
        //当接收到消息时，调用cutDown()方法。
        //latch.countDown();
        //消息处理函数
        handleMessage(message,channel);
    }

    public void handleMessage(String message, String channel){
        if("jedis.user".equals(channel)){
            userMessageCustomer.setMessage(message);
            threadPool.execute(userMessageCustomer);
        }
    }
}
